import java.util.HashMap;
import java.util.List;

public class SudokuSolutionVerifier {

    HashMap<String, String> lookup = new HashMap<String, String>();

    public SudokuSolutionVerifier(BacktrackingSolver bts,
            List<List<List<Integer>>> puzzles) {

        for (List<List<Integer>> puzzle : puzzles) {
            String flatOrigPuzzle = flattenToString(puzzle);
            String flatSolPuzzle = flattenToString(bts.solve(puzzle));
            lookup.put(flatSolPuzzle, flatOrigPuzzle);
        }

    }

    private String flattenToString(List<List<Integer>> list) {
        StringBuilder str = new StringBuilder(81);
        for (List<Integer> row : list) {
            for (Integer col : row) {
                str.append(col);
            }
        }
        return str.toString();
    }

    public boolean isSolved(List<List<Integer>> puzzle,
            List<List<Integer>> solution) {
        String flatSolPuzzle = flattenToString(solution);
        String query;
        if ((query = lookup.get(flatSolPuzzle)) != null) {
            // solution exists, also chk for correct puzzle.
            return query.equals(flattenToString(puzzle));
        }
        // solution doesn't exist.
        return false;

    }
}
